#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
Simplified BSD License (BSD)
Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this 
      list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, 
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 05-Nov-09 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;
using System.Drawing;
using System.Drawing.Drawing2D;

using Thn;
using Thn.Drawing;
using Thn.Drawing.Painters;
#endregion

namespace Thn.Interface.Vcl.Themes.BlueSea
{
    /// <summary>
    /// Responsible for rendering visual aspects of <see cref="Calendar"/>.
    /// </summary>
#if DEBUG
    public
#else
    public
#endif
 class CalendarRenderer : BlueThemeRendererBase
    {
        #region Fields

        #endregion

        #region On Render Content
        /// <summary>
        /// Paint the control's content (text, value, data, etc.) onto canvas.
        /// </summary>
        /// <param name="control">The control to be rendered.</param>
        /// <param name="canvas">The canvas on which to paint the control.</param>
        /// <param name="context">The current drawing context.</param>
        protected override void OnRenderContent(IControl control, Graphics canvas, RenderContext context)
        {
            CalendarRenderContext rc = (CalendarRenderContext)context;
            Calendar con = (Calendar)control;
            ITheme theme = con.EffectiveTheme;
            IFontStyle fontNormal = theme.FindFont(StyleNames.CalendarNormalDay);
            IFontStyle fontSunday = theme.FindFont(StyleNames.CalendarSunday);
            IFontStyle fontOther = theme.FindFont(StyleNames.CalendarOtherDay);
            IFontStyle fontToday = theme.FindFont(StyleNames.CalendarToday);
            IFontStyle font = null;
            Image imgToday = theme.FindImage(ImageNames.Calendar_Today);
            Image imgSelected = theme.FindImage(ImageNames.Calendar_Selected);
            int[] cols = rc.Columns;
            int[] rows = rc.Rows;
            int x1 = 0;
            int x2 = 0;
            int y1 = 0;
            int y2 = 0;
            int idx = 0;
            Rectangle rect;
            string txt = "";

            #region Render titles
            DateTime displayDate = rc.DisplayDate;
            txt = string.Format("{0} / {1}", displayDate.Month, displayDate.Year);
            if (con.Focused) font = theme.FindFont(StyleNames.CalendarTitleFocused);
            else font = theme.FindFont(StyleNames.CalendarTitle);
            if (font != null)
            {
                font.DrawString(txt, canvas, rc.TitleRegion, ContentAlignment.MiddleCenter, false);
            }
            #region Defensive tracing
            else
            {
                //font style not yet assigned
            }
            #endregion

            #endregion

            #region Render today
            if ((imgToday != null) && (fontToday != null))
            {
                //draw icon
                y1 = rc.TodayRegion.Height / 2 - imgToday.Height / 2 + rc.TodayRegion.Top;
                x1 = rc.TodayRegion.Left + 5;
                rect = new Rectangle(x1, y1, imgToday.Width, imgToday.Height);
                canvas.DrawImage(imgToday, x1, y1);

                //draw today as text
                DateTime today = DateTime.Now;
                txt = today.ToString(UI.Culture.DateTimeFormat.ShortDatePattern);
                rect = new Rectangle(rect.Right + 5, rect.Top, rc.TodayRegion.Width - rect.Width - 10, rect.Height);
                fontToday.DrawString(txt, canvas, rect, ContentAlignment.MiddleLeft, false);
            }
            #region Defensive tracing
            else
            {
                //image Today is not found.
            }
            #endregion

            #endregion

            #region Render dates
            y1 = rows[0];
            bool isOther = false;
            for (int r = 0; r < 6; r++)
            {
                x1 = cols[0];
                y2 = rows[r + 1];

                for (int c = 0; c < 7; c++)
                {
                    x2 = cols[c + 1];
                    rect = new Rectangle(x1, y1, x2 - x1, y2 - y1);
                    //canvas.DrawRectangle(Pens.DodgerBlue, x1, y1, x2 - x1, y2 - y1);

                    #region Render Selection
                    if ((idx == rc.SelectedIndex) && (imgSelected != null))
                    {
                        canvas.DrawImage(imgSelected, (x1 + x2 - imgSelected.Width) / 2, (y1 + y2 - imgSelected.Height) / 2);
                    }
                    #endregion

                    #region Render Today
                    if ((idx == rc.TodayIndex) && (imgToday != null))
                    {
                        canvas.DrawImage(imgToday, (x1 + x2 - imgToday.Width) / 2, (y1 + y2 - imgToday.Height) / 2);
                    }
                    #endregion

                    #region Select appropriate font
                    isOther = false;
                    if (r < rc.StartRow) isOther = true;
                    else if (r > rc.EndRow) isOther = true;
                    else if (r == rc.StartRow)
                    {
                        isOther = c < rc.StartColumn;
                    }
                    else if (r == rc.EndRow)
                    {
                        isOther = c > rc.EndColumn;
                    }
                    else
                    {//this month's normal dates
                        font = fontNormal;
                    }

                    if (isOther) font = fontOther;
                    else if (c == 0) font = fontSunday;
                    else font = fontNormal;

                    #endregion

                    #region Render text
                    if (font != null)
                    {
                        font.DrawString(rc.Dates[r][c].ToString(), canvas, rect, ContentAlignment.MiddleCenter, false);
                    }
                    #region Defensive tracing
                    else
                    {
                        //font not assigned
                    }
                    #endregion

                    #endregion

                    //prepare for next column
                    x1 = x2;
                    idx++;
                }

                //prepare for next row
                y1 = y2;
            }
            #endregion

            #region Debug - render debug guides
            //canvas.DrawRectangle(Pens.Red, rc.HeaderRegion);
            //canvas.DrawRectangle(Pens.Pink, rc.TitleRegion);
            //canvas.DrawRectangle(Pens.Blue, rc.DaysRegion);
            //canvas.DrawRectangle(Pens.Green, rc.TodayRegion);
            //canvas.DrawRectangle(Pens.Orange, rc.CalendarRegion);

            //canvas.DrawRectangle(Pens.DodgerBlue, rc.NextMonthRegion);
            //canvas.DrawRectangle(Pens.DodgerBlue, rc.PreviousMonthRegion);
            //canvas.DrawRectangle(Pens.GreenYellow, rc.NextYearRegion);
            //canvas.DrawRectangle(Pens.GreenYellow, rc.PreviousYearRegion);

            //paint dates grid
            int gridX1 = rc.Columns[0];
            int gridY1 = rc.Rows[0];
            int gridX2 = rc.Columns[rc.Columns.Length - 1];
            int gridY2 = rc.Rows[rc.Rows.Length - 1];
            //for (int i = 0; i < rc.Columns.Length - 1; i++)
            //{
            //    canvas.DrawLine(Pens.Gray, rc.Columns[i], gridY1, rc.Columns[i], gridY2);
            //}
            //for (int i = 0; i < rc.Rows.Length - 1; i++)
            //{
            //    canvas.DrawLine(Pens.Gray, gridX1, rc.Rows[i], gridX2, rc.Rows[i]);
            //}
            #endregion
        }
        #endregion

        #region On Render Visual
        /// <summary>
        /// Paint the control's visual stuff (border, background, etc.) onto canvas.
        /// </summary>
        /// <param name="control">The control to be rendered.</param>
        /// <param name="canvas">The canvas on which to paint the control.</param>
        /// <param name="context">The current drawing context.</param>
        protected override void OnRenderVisual(IControl control, Graphics canvas, RenderContext context)
        {
            CalendarRenderContext rc = (CalendarRenderContext)context;
            Calendar con = (Calendar)control;
            PainterBase painter = null;
            ITheme theme = con.EffectiveTheme;

            //draw header region
            //if (con.Focused) painter = new OrangeGloss();
            //else painter = new DeepBlueGloss();
            if (con.Focused) painter = new DeepBlueGloss();
            else if (rc.IsMouseOver) painter = new LightBlueGloss();
            else painter = new BlueGrayGloss();
            painter.PaintRectangle(canvas, rc.HeaderRegion, true, true);

            //draw body region
            painter = new WhiteGloss();
            ((GlossPainterBase)painter).Ratio = 1.0 / 3.0;
            Rectangle r = new Rectangle(rc.HeaderRegion.Left, rc.HeaderRegion.Bottom + 1, rc.HeaderRegion.Width, rc.BodyRegion.Bottom - rc.HeaderRegion.Bottom - 2);
            painter.PaintRectangle(canvas, r, true, true);

            #region Render buttons

            Image imgUp = null;
            Image imgDown = null;

            //if (con.Focused)
            //{
            //    imgUp = theme.FindImage(ImageNames.UpOver);
            //    imgDown = theme.FindImage(ImageNames.DownOver);
            //}
            //else
            //{
            //    imgUp = theme.FindImage(ImageNames.Up);
            //    imgDown = theme.FindImage(ImageNames.Down);
            //}

            if ((con.Focused) || (rc.IsMouseOver))
            {
                imgUp = theme.FindImage(ImageNames.Up);
                imgDown = theme.FindImage(ImageNames.Down);
            }
            else
            {

            }

            GraphicsState state = canvas.Save();
            canvas.InterpolationMode = InterpolationMode.High;

            if (imgUp != null)
            {
                canvas.DrawImage(imgUp, rc.NextMonthRegion);
                canvas.DrawImage(imgUp, rc.NextYearRegion);
            }
            #region Defensive tracing
            else
            {
                //image is not assigned
            }
            #endregion

            if (imgDown != null)
            {
                canvas.DrawImage(imgDown, rc.PreviousMonthRegion);
                canvas.DrawImage(imgDown, rc.PreviousYearRegion);
            }
            #region Defensive tracing
            else
            {
                //image is not assigned
            }
            #endregion

            canvas.Restore(state);

            #endregion
        }
        #endregion

        #region Constructors
        /// <summary>
        /// Default constructor.
        /// </summary>
        public CalendarRenderer()
        {
            mDrawVisualCues = false;
        }
        #endregion
    }
}
